/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Island : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Texture2D _texture;
        private SpriteBatch _spriteBatch;
        private Rectangle _spriteRectangle;
        private Vector2 _position;
        //private Vector2 _speed;
        //private Rectangle frame;
        private List<Rectangle> frames;
        public Island(Game game, ref Texture2D texture)
            : base(game)
        {
            _texture = texture;
            _spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            
            Frames = new List<Rectangle>();
            Rectangle frame = new Rectangle();
            frame.X = 103;
            frame.Y = 499;
            frame.Width = 64;
            frame.Height = 65;
            Frames.Add(frame); //index 0 = island 1
            
            frame.X = 168;
            Frames.Add(frame); //index 1 = island 2
            
            frame.X = 233;
            Frames.Add(frame); //index 2 = island 3
            
            _spriteRectangle = Frames.ElementAt(0);
            _position.X = Util.Instance.RandomGenerator.Next(Util.ScreenWidth);
            //_position.X = 200;
            _position.Y = 200;


        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (_position.Y > Util.ScreenHeight)
            {
                randIsland();
                _position.Y = -65;
            }
            _position.Y += 1;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_texture, _position, _spriteRectangle, Color.White);
            _spriteBatch.End();
            base.Draw(gameTime);
        }


        private List<Rectangle> Frames
        {
            get { return frames; }
            set { frames = value; }
        }

        private void randIsland()
        {
            _spriteRectangle = Frames.ElementAt(Util.Instance.RandomGenerator.Next(2));
            _position.X = Util.Instance.RandomGenerator.Next(Util.ScreenWidth);
        }

    }
}